# -*- coding: utf-8 -*-
"""
oob和关于bagging的更多讨论
Created on Sat Apr 28 15:22:09 2018

@author: Allen
"""
import numpy as np
import matplotlib.pyplot as plt

from sklearn import datasets

X, y = datasets.make_moons( n_samples = 500, noise = 0.3, random_state = 666 )

plt.scatter( X[y==0,0], X[y==0,1] )
plt.scatter( X[y==1,0], X[y==1,1] )
plt.show()


from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split( X, y, random_state = 666 )

'''
使用 oob
'''
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import BaggingClassifier

bagging_clf = BaggingClassifier( DecisionTreeClassifier(),
                                 n_estimators = 500,
                                 max_samples = 100,
                                 bootstrap = True,
                                 oob_score = True
                                )
bagging_clf.fit( X, y )
print( bagging_clf.oob_score_ ) # 0.92

'''
还可以传入n_jobs,来指定使用若干个cpu核来进行计算
'''


### bootstrap_features 特征空间 随机采样
random_subspaces_clf = BaggingClassifier( DecisionTreeClassifier(),
                                 n_estimators = 500,
                                 max_samples = 500,
                                 bootstrap = True,
                                 oob_score = True,
                              
                                 max_features = 1,
                                 bootstrap_features = True
                                )
'''
max_features 最大拿出的特征数量
bootstrap_features 对特征随机采样，方式是放回采样
关闭随机样本采样的方法，将max_samples 设置为何样本数量一样多，这样就不会再随机了
'''
random_subspaces_clf.fit( X, y )
print( random_subspaces_clf.oob_score_ ) # 0.836


random_patches_clf = BaggingClassifier( DecisionTreeClassifier(),
                                 n_estimators = 500,
                                 max_samples = 100,
                                 bootstrap = True,
                                 oob_score = True,
                              
                                 max_features = 1,
                                 bootstrap_features = True
                                )
'''
max_features 最大拿出的特征数量
bootstrap_features 对特征随机采样，方式是放回采样
关闭随机样本采样的方法，将max_samples 设置为何样本数量一样多，这样就不会再随机了
'''
random_patches_clf.fit( X, y )
print( random_patches_clf.oob_score_ ) # 0.868

'''
对于这些大量的子模块，还有一个特别的名字——随机森林
'''